//
//  DeleteViewController.m
//  03 PhotoKit
//
//  Created by liyoubing on 16/5/24.
//  Copyright © 2016年 liyoubing. All rights reserved.
//

#import "DeleteViewController.h"
#import "ImageCell.h"
#import <Photos/Photos.h>

@interface DeleteViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout> {
    
    UICollectionView *_collectionView;
    PHFetchResult *_result;
    PHImageManager *_manager;
}

@end

@implementation DeleteViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self _initCollectionView];
    
    //加载数据
    [self _loadData];
}

//加载数据
- (void)_loadData {

    PHFetchResult *resultCollection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
    
//    for (int i=0; i<resultCollection.count; i++) {
//        NSLog(@"result:%@",resultCollection[i]);
//    }
    
    //获取最近删除的colleciton
    PHAssetCollection *collection = resultCollection[0];
    
    _result = [PHAsset fetchKeyAssetsInAssetCollection:collection options:nil];
    
    _manager = [PHImageManager defaultManager];
}

- (void)_initCollectionView {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(90, 90);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    
    [_collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"ImageCell"];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _result.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImageCell" forIndexPath:indexPath];
    
    [_manager requestImageForAsset:_result[indexPath.row] targetSize:CGSizeMake(90, 90) contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
        
        cell.img = result;
        
    }];
    
    
    return cell;
}



@end
